/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data_bases.beans;

/**
 *
 * @author chucho
 */
import data_bases.DAO.DatabaseDAO;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import data_bases.business.DataBaseBusiness;
import data_bases.business.Schema;
import data_bases.business.SchemaDataModel;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import privileges.business.UserBusiness;

@ManagedBean
@SessionScoped
public class TableBean implements Serializable {
    
    private List<ArrayList<String>> table;
    private List<String> columnsNames;
    private String bdname;
    private String value;
    private String message;
    private int index;
    private String disabled;
    private String disabledParent;
    private String schemaName;
    private List<Schema> schemas;
    private List<String> collates;
    private List<String> tables;
    private List<String> indexs;
    private List<String> procedures;
    private List<String> functions;
    private List<String> triggers;
    private Schema selectedSchema;
    private Schema [] selectedSchemas;
    private SchemaDataModel mediumSchemasModel;
    
    public TableBean() {
        index = 0;
        disabled = "true";
        value = "iniciar";
        reloadTables();

    }
    private void reloadTables() {
        bdname = "";
        selectedSchema = null;
        try {
            try {
                collates = new DataBaseBusiness(new DatabaseDAO().collation()).getCollate();
            } catch (SQLException ex) {
                Logger.getLogger(TableBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            schemas = new DataBaseBusiness(new DatabaseDAO().viewDatabases()).getSchemas();
        } catch (SQLException ex) {
            Logger.getLogger(TableBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        mediumSchemasModel = new SchemaDataModel(schemas);

    }

    public List<String> getTables() {
        return tables;
    }

    public void setTables(List<String> tables) {
        this.tables = tables;
    }

    public List<String> getIndexs() {
        return indexs;
    }

    public void setIndexs(List<String> indexs) {
        this.indexs = indexs;
    }

    public List<String> getProcedures() {
        return procedures;
    }

    public void setProcedures(List<String> procedures) {
        this.procedures = procedures;
    }

    public List<String> getFunctions() {
        return functions;
    }

    public void setFunctions(List<String> functions) {
        this.functions = functions;
    }

    public List<String> getTriggers() {
        return triggers;
    }

    public void setTriggers(List<String> triggers) {
        this.triggers = triggers;
    }

    public String getSchemaName() {
        return schemaName;
    }

    public void setSchemaName(String schemaName) {
        this.schemaName = schemaName;
    }

    public String getDisabledParent() {
        return disabledParent;
    }

    public void setDisabledParent(String disabledParent) {
        this.disabledParent = disabledParent;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getDisabled() {
        return disabled;
    }

    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public Schema getSelectedSchema() {
        return selectedSchema;
    }

    public void setSelectedSchema(Schema selectedSchema) {
        this.selectedSchema = selectedSchema;
    }

    public Schema[] getSelectedSchemas() {
        return selectedSchemas;
    }

    public void setSelectedSchemas(Schema[] selectedSchemas) {
        this.selectedSchemas = selectedSchemas;
    }

    public SchemaDataModel getMediumSchemasModel() {
        return mediumSchemasModel;
    }

    public void setMediumSchemasModel(SchemaDataModel mediumSchemasModel) {
        this.mediumSchemasModel = mediumSchemasModel;
    }

    public List<String> getCollates() {
        return collates;
    }

    public void setCollates(List<String> collates) {
        this.collates = collates;
    }
    
    public List<Schema> getSchemas() {
        return schemas;
    }

    public void setSchemas(List<Schema> schemas) {
        this.schemas = schemas;
    }
    
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    
    public String getBdname() {
        return bdname;
    }

    public void setBdname(String bdname) {
        this.bdname = bdname;
    }
    
    public List<ArrayList<String>> getTable() {
        return table;
    }

    public void setTable(List<ArrayList<String>> table) {
        this.table = table;
    }

    public List<String> getColumnsNames() {
        return columnsNames;
    }

    public void setColumnsNames(List<String> columnsNames) {
        this.columnsNames = columnsNames;
    }
    
    public void createDB() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (bdname.equals("")) {
            message = "No dio ningún nombre";
            context.addMessage(null, new FacesMessage("Error!!", message));
            //return "";
        } else {
            if (value.equals("")) {
                try {
                    new DatabaseDAO().createDatabase(bdname);
                    message = "Schema Creado!";
                    selectedSchema = null;
                    reloadTables();
                    context.addMessage(null, new FacesMessage("Exito!!", message));
                    //return "principalPage.xhtml?faces-redirect=true";
                } catch (SQLException ex) {
                    message = "Ocurrio un Error -->" + "ErrorCode:" + ex.getLocalizedMessage();
                    context.addMessage(null, new FacesMessage("Error!!", message));
                    //return "";
                }
            } else {
                try {
                    new DatabaseDAO().createDatabase(bdname, value);
                    selectedSchema = null;
                    message = "Schema Creado!";
                    reloadTables();
                    context.addMessage(null, new FacesMessage("Exito!!", message));
                    //return "principalPage.xhtml?faces-redirect=true";
                } catch (SQLException ex) {
                    message = "Ocurrio un Error -->" + "ErrorCode:" + ex.getLocalizedMessage();
                    context.addMessage(null, new FacesMessage("Error!!", message));
                    //return "";
                }
            }
        }
    }
    
    public void drop() {
        FacesContext context = FacesContext.getCurrentInstance();
        String[] schem = new String[1];
        if (schemaName == null) {
            message = "No selecciono ningun Schema!";
            context.addMessage(null, new FacesMessage("Error!!", message));
            //return "";
        } else {
            schem[0] = schemaName;
            System.out.println(schem[0]);
            try {
                new DatabaseDAO().deleteDatabases(schem);
                message = "Schema Borrado!";
                index = 0;
                disabledParent = "false";
                disabled = "true";
                selectedSchema = null;
                reloadTables();
                context.addMessage(null, new FacesMessage("Exito!!", message));
                //return "principalPage.xhtml?faces-redirect=true";
            } catch (SQLException ex) {
                ex.printStackTrace();
                message = "Ocurrio un Error -->" + "ErrorCode:" + ex.getLocalizedMessage();
                context.addMessage(null, new FacesMessage("Error!!", message));
                //return "";
            }
        }
    }
    
    public void singleDrop() {
            FacesContext context = FacesContext.getCurrentInstance();
        System.out.println("entro aca");
        String[] schem = new String[1];
        //System.out.println(selectedSchema.getName());
        if (selectedSchema == null) {
            message = "No selecciono ningun Schema!";
            context.addMessage(null, new FacesMessage("Error!!", message));
            //return "";
        } else {
            schem[0] = selectedSchema.getName();
            try {
                new DatabaseDAO().deleteDatabases(schem);
                message = "Schema Borrado!";
                //index = 0;
                //disabledParent = "false";
                //disabled = "true";
                //selectedSchema = null;
                //return "principalPage.xhtml?faces-redirect=true";
                reloadTables();
                context.addMessage(null, new FacesMessage("Exito!!", message));
            } catch (SQLException ex) {
                message = "Ocurrio un Error -->" + "ErrorCode:" + ex.getLocalizedMessage();
                context.addMessage(null, new FacesMessage("Error!!", message));
                //return "";
            }
        }
    }
    
    public void changeTab(){
        FacesContext context = FacesContext.getCurrentInstance();  
        if(selectedSchema == null)
            context.addMessage(null,new FacesMessage("Error ","Por favor seleccione un Scchema"));
        else{
            disabledParent = "true";
            disabled = "false";
            index++;
            schemaName = selectedSchema.getName();
            tables = new DataBaseBusiness().getDatabaseTables(schemaName);
            indexs = new DataBaseBusiness().getDatabaseIndex(schemaName);
            procedures = new DataBaseBusiness().getDatabaseProcedures(schemaName);
            triggers = new DataBaseBusiness().getDatabaseTriggers(schemaName);
            functions = new DataBaseBusiness().getDatabaseFunctions(schemaName);
            context.addMessage(null,new FacesMessage("Exito","Usted seleccione el schema "+ schemaName));
         
        }
    }
    
    public String cancel(){
        index = 0;
        disabledParent = "false";
        disabled = "true";
        selectedSchema = null;
        FacesContext fc = FacesContext.getCurrentInstance();
        if (fc.getExternalContext().getSessionMap().containsKey("tableBean")) {
            fc.getExternalContext().getSessionMap().remove("tableBean");
        }
        return "principalPage.xhtml?faces-redirect=true";
    }
    
    
}
